package com.uva.pgpi.sgcc.preload;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.uva.pgpi.sgcc.beans.Rol;
import com.uva.pgpi.sgcc.util.DataBaseConnection;

public class CrearRoles {

	public static void listRoles() throws SQLException {

		ConnectionSource con = DataBaseConnection.getInstance().getConnection();

		Dao<Rol, String> rolDao = DaoManager.createDao(con, Rol.class);

		Iterator<Rol> it = rolDao.queryForAll().iterator();

		while (it.hasNext()) {
			System.out.println(it.next().toString());
		}
	}

	public static void main(String[] args) throws SQLException,
			NoSuchAlgorithmException, InvalidKeySpecException {

		ConnectionSource connectionSource = DataBaseConnection.getInstance()
				.getConnection();
		Dao<Rol, String> rolDao = DaoManager.createDao(connectionSource,
				Rol.class);

		// TableUtils.dropTable(connectionSource, Rol.class, true);
		TableUtils.createTableIfNotExists(connectionSource, Rol.class);

		ArrayList<Rol> roles = new ArrayList<Rol>();

		roles.add(new Rol("Solicitante"));
		roles.add(new Rol("CCC"));
		roles.add(new Rol("Trabajador"));
		roles.add(new Rol("Probador"));

		for (int i = 0; i < roles.size(); i++) {
			rolDao.createIfNotExists(roles.get(i));
		}

		connectionSource.close();

		listRoles();
	}

}
